using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.ApiRead;
using B.S.BasicData.Infrature;
using System.Linq;
using Microsoft.EntityFrameworkCore;

public class ProindexinfoPagingDtoGetByIdHandler : IRequestHandler<ProindexinfoPagingDtoGetByIdRequest, ApiResult<ProindexinfoPagingDto>>
{
    private readonly AppDbContext _dbContext;
    public ProindexinfoPagingDtoGetByIdHandler(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task<ApiResult<ProindexinfoPagingDto>> Handle(ProindexinfoPagingDtoGetByIdRequest request, CancellationToken cancellationToken)
    {
        var query = from p in _dbContext.proindexinfos
                    where p.Id == request.Id && !p.IsDeleted
                    join pn in _dbContext.ProductNames on p.ProductNameId equals pn.Id into gj
                    from pn in gj.DefaultIfEmpty()
                    select new ProindexinfoPagingDto
                    {
                        Id = p.Id,
                        ProductNameId = p.ProductNameId,
                        ProductName = pn != null ? pn.Name : string.Empty,
                        BatchNo = p.BatchNo,
                        ShelfLife = p.ShelfLife,
                        LicenseNo = p.LicenseNo,
                        Manufacturer = p.Manufacturer,
                        CoverImage = p.CoverImage,
                        InfoCompleteness = p.InfoCompleteness,
                        IsDeleted = p.IsDeleted
                    };
        var result = await query.FirstOrDefaultAsync();
        if (result == null)
            return ApiResult<ProindexinfoPagingDto>.Fail("未找到数据");
        return ApiResult<ProindexinfoPagingDto>.Success(result, "查询成功");
    }
} 